// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
    try {
        const { 
         derivativeId, 
          page = 1, 
          pageSize = 10 
        } = event;
        
        if (!derivativeId) {
          return {
            success: false,
            message: '参数缺少'
          };
        }
        
        const skip = (page - 1) * pageSize;
        
        // 构建查询条件
        let query = {
            status:true,
            type:'derivative',
            ipId:derivativeId
        };
        
        // 执行查询
        const goodsResult = await db.collection('blindbox_goods')
          .where(query)
          .orderBy('createTime', 'desc')
          .skip(skip)
          .limit(pageSize)
          .get();
        
        // 获取总数量
        const countResult = await db.collection('blindbox_goods')
          .where(query)
          .count();
        
        return {
          success: true,
          data: goodsResult.data,
          total: countResult.total
        };
      } catch (e) {
        console.error('获取衍生品失败', e);
        return {
          success: false,
          message: '获取衍生品失败',
          error: e
        };
      }
}